<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/10-12:02
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\service\CategoryService;
    use app\admin\validate\AlbumValidate;
    use Jrk\Random;
    use Jrk\Trees;
    use think\Exception;


    class Album extends AdminBase
    {


        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub

            $this->model = app()->model("Albums");

            $this->assign("tree", Trees::toFormatTree(CategoryService::selcectAll(), 'name'));

            $this->del = "相册";
        }


        /**
         * @return mixed
         * @throws Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addAlbum
         * @describe:
         */
        public function addAlbum()
        {
            if (IS_POST) {
                $data = $this->request->post();

                if (!isset($data['images']) || !is_array($data['images'])) {
                    $this->error("最少上传一张图片");
                } else {
                    sort($data['images']);
                }

                $validate = new AlbumValidate();

                if (!$validate->check($data)) {
                    $this->error($validate->getError());
                }

                //封面
                $data['img'] = $data['images'][0];

                $data['nums'] = count($data['images']);

                //图集
                $data['images'] = serialize($data['images']);
                //密码
                if ($data['is_open'] == 1) {
                    if (empty($data['password'])) {
                        $data['password'] = Random::alnum(6);
                    }
                }

                if (isset($data['id'])) {
                    addLog("相册修改操作", "修改相册：".$data['name']);
                } else {
                    addLog("相册添加操作", "新增相册：".$data['name']);
                }
                return $this->model->addAndEdit($data);

            }

            return $this->fetch();
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/10
         * @name: editAlbum
         * @describe:编辑
         */
        public function editAlbum()
        {
            /** @var TYPE_NAME $id */
            $id = $this->request->param("id/d");//父id

            $info = $this->model->getOne(['id' => $id]);

            if (empty($info)) {
                $this->assign('msg', '当前id:'.$id.'错误');
                $this->fetch("public/404");
            }
            //图集格式转换
            $info['images'] = unserialize($info['images']);


            $info['tg']=empty($info['tags'])?[]:explode(",",$info['tags']);


            $this->assign(compact("info"));

            return $this->fetch();
        }



        /**
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/10
         * @name: passLock
         * @describe:验证密码
         */
        public function passLock()
        {
            if (IS_POST) {
                $data = $this->request->post();
                $info = $this->model->getOne(['id' => $data['id']]);
                if ($info['password'] == $data['password']) {
                    $this->success("密码正确");
                } else {
                    $this->error("密码错误");
                }
            }
        }



        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/10
         * @name: addImages
         * @describe:相册添加图片
         */
        public function addImages()
        {
            if (IS_POST) {
                $data = $this->request->post();
                if (intval($data['id']) == 0) {
                    $this->error("请选择相册");
                }
                try {
                    $pre  = db("album")->where("id", intval($data['id']))->field("nums,images")->find();
                    $img  = explode(",", $data['img']);
                    $pimg = unserialize($pre['images']);
                    $kf   = array_merge($img, $pimg);
                    $num  = count($kf);
                    $imgs = serialize($kf);

                    $arr = [
                        'id'     => intval($data['id']),
                        'nums'   => $num,
                        'images' => $imgs,
                    ];
                    addLog("相册图片添加操作", "相册新增图片");

                    return $this->model->addAndEdit($arr);

                } catch (Exception $exception) {

                    $this->error($exception->getMessage());
                }
            }

            $data = $this->model->where("is_show", 1)->order("id desc")->select();

            $this->assign("data", $data);

            return $this->fetch();

        }


        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: viewImages
         * @describe:
         */
        public function viewImages(){

            $id = $this->request->param("id/d");//父id

            $info = $this->model->getOne(['id' => $id]);

            if (empty($info)) {
                $this->assign('msg', '当前id:'.$id.'错误');
                $this->fetch("public/404");
            }

            //图集格式转换
            $data= unserialize($info['images']);

            $this->assign(compact("data"));
            return $this->fetch();
        }




        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/10
         * @name: removeCategory
         * @describe:移动
         */
        public function removeCategory()
        {
            if (IS_POST) {
                $param = $this->request->param();

                return $this->model->removeCategory($param['ids'], $param['category_id']);
            }
        }


    }